/**
 * 
 */
package com.deitel.advjhtp1.gui.mdi;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;

import com.deitel.advjhtp1.gui.webbrowser.WebBrowserPane;
import com.deitel.advjhtp1.gui.webbrowser.WebToolBar;

/**
 * @author Aslanbek
 *
 */
public class MDIWebBrowser extends JFrame {

	private static final long serialVersionUID = -6428255979241068243L;
	
	JDesktopPane desktopPane = new JDesktopPane();
	
	public MDIWebBrowser()
	{
		super("MDI Web Browser");
		
		createNewWindow();
		
		Container contentPane = getContentPane();
		contentPane.add(desktopPane);
		
		JMenu fileMenu = new JMenu("File");
		fileMenu.add(new NewWindowAction());
		fileMenu.addSeparator();
		fileMenu.add(new ExitAction());
		fileMenu.setMnemonic('F');
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		setJMenuBar(menuBar);
	}

	private void createNewWindow() {
		
		JInternalFrame frame = new JInternalFrame(
				"Browser",
				true,
				true,
				true,
				true);
		
		WebBrowserPane browserPane = new WebBrowserPane();
		WebToolBar toolBar = new WebToolBar(browserPane);
		
		Container contentPane =  getContentPane();
		contentPane.add(toolBar,BorderLayout.NORTH);
		contentPane.add(new JScrollPane(browserPane),BorderLayout.CENTER);
		frame.setSize(640, 480);
		int offset = 30 * desktopPane.getAllFrames().length;
		frame.setLocation(offset, offset);
		
		desktopPane.add(frame);
		frame.setVisible(true);
	}
	
	private class NewWindowAction extends AbstractAction
	{
		private static final long serialVersionUID = 1L;

		public NewWindowAction()
		{
			putValue(Action.NAME, "New Window");
			putValue(Action.SHORT_DESCRIPTION, "Create New Browser Window");
			putValue(Action.MNEMONIC_KEY, new Integer('N'));
		}
		
		public void actionPerformed(ActionEvent event)
		{
			createNewWindow();
		}
	}
	
	private class ExitAction extends AbstractAction
	{
		private static final long serialVersionUID = -1641825671307034886L;
		public ExitAction()
		{
			putValue(Action.NAME, "Exit");
			putValue(Action.SHORT_DESCRIPTION, "Exit Application");
			putValue(Action.MNEMONIC_KEY, new Integer('x'));
		}
		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
		
	}
	
	public static void main(String args[])
	{
		MDIWebBrowser browser = new MDIWebBrowser();
		browser.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		browser.setSize(640, 480);
		browser.setVisible(true);
	}

}
